package com.epower.web.action.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.epower.base.BaseAction;
import com.epower.bo.epower.commons.Result;
import com.epower.bo.epowerbook.EpowerBookService;
import com.epower.dataobject.EpowerBookDO;

/**
 * @author bolin.hbc
 * 
 * 
 *         课本的action
 * 
 */
@Scope("prototype")
@Component("book")
public class BookAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private EpowerBookService epowerBookBO;

	private Integer id;
	private Integer liblevel;
	private Result result = Result.succResult();

	public String doGetBookById() {

		if (id == null) {
			result = Result.errorResult(Result.RESULTCODE_REQUEST_FAILURE, "id can't be null");
			return ERROR;
		}

		EpowerBookDO book = epowerBookBO.getEpowerBookById(id);

		if (book == null) {
			result = Result.errorResult(Result.RESULTCODE_REQUEST_FAILURE, "book is not exist");
			return ERROR;
		}

		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("bookid", book.getId());
		resultMap.put("pid", book.getPid());
		resultMap.put("sortnum", book.getSortnum());
		resultMap.put("liblevel", book.getLiblevel());
		resultMap.put("libname", book.getLibname());
		resultMap.put("quantity", book.getQuantity());
		resultMap.put("desc", book.getDescription());
		result.setResult(resultMap);
		return SUCCESS;
	}

	public String doGetChildsById() {
		EpowerBookDO epowerBook = new EpowerBookDO();
		epowerBook.setPid(id);
		List<EpowerBookDO> bookList = epowerBookBO.getEpowerBookIdsByOption(epowerBook);

		if (!bookList.isEmpty()) {
			List<TmpBook> tmpList = new ArrayList<TmpBook>();
			for (EpowerBookDO book : bookList) {
				TmpBook tmp = new TmpBook();
				tmp.setBookid(book.getId());
				tmp.setLibname(book.getLibname());
				tmp.setBook(epowerBookBO.isBook(book.getId()));
				tmpList.add(tmp);
			}
			Map<String, Object> resultMap = new HashMap<String, Object>();
			resultMap.put("books", tmpList);
			result.setResult(resultMap);
		}
		return SUCCESS;
	}

	public String doGetBooksByLevel() {

		if (liblevel == null) {
			result = Result.errorResult(Result.RESULTCODE_REQUEST_FAILURE, "liblevel cant't be null");
			return ERROR;
		}
		EpowerBookDO epowerBook = new EpowerBookDO();
		epowerBook.setLiblevel(liblevel);
		List<EpowerBookDO> bookList = epowerBookBO.getEpowerBookIdsByOption(epowerBook);
		if (!bookList.isEmpty()) {
			List<TmpBook> tmpList = new ArrayList<TmpBook>();
			for (EpowerBookDO book : bookList) {
				TmpBook tmp = new TmpBook();
				tmp.setBookid(book.getId());
				tmp.setLibname(book.getLibname());
				tmp.setBook(epowerBookBO.isBook(book.getId()));
				tmpList.add(tmp);
			}
			Map<String, Object> resultMap = new HashMap<String, Object>();
			resultMap.put("books", tmpList);
			result.setResult(resultMap);
		}
		return SUCCESS;
	}

	public void setEpowerBookBO(EpowerBookService epowerBookBO) {
		this.epowerBookBO = epowerBookBO;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getLiblevel() {
		return liblevel;
	}

	public void setLiblevel(Integer liblevel) {
		this.liblevel = liblevel;
	}

	public Result getResult() {
		return result;
	}

	public void setResult(Result result) {
		this.result = result;
	}

	public class TmpBook {

		private Integer bookid;
		private String libname;
		private boolean isBook;

		public boolean isBook() {
			return isBook;
		}

		public void setBook(boolean isBook) {
			this.isBook = isBook;
		}

		public Integer getBookid() {
			return bookid;
		}

		public void setBookid(Integer bookid) {
			this.bookid = bookid;
		}

		public String getLibname() {
			return libname;
		}

		public void setLibname(String libname) {
			this.libname = libname;
		}

	}
}
